package gspushmq.v1.app.subscribeapp.utils;


import org.apache.commons.lang3.StringUtils;

import gspushmq.v1.app.subscribeapp.enumeration.ConsumerEnum;
import gspushmq.v1.app.subscribeapp.enumeration.LinkEnum;
import gspushmq.v1.app.subscribeapp.enumeration.ProducerEnum;
import gspushmq.v1.app.subscribeapp.enumeration.ProductLineEnum;
import gspushmq.v1.app.subscribeapp.enumeration.PushTypeEnum;
import gspushmq.v1.app.subscribeapp.models.BaseVO;

/**
 * @Description 参数校验工具类
 * @Author chengsj
 * @Date 2018/9/21 11:37
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class ValidateHelper {
    public static void validate(BaseVO object) throws Exception {
        if (object == null)
            throw new Exception("消息对象异常！");

        if (!ProductLineEnum.contain(object.getSource()))
            throw new Exception("产品线code错误!");

        if ((!ConsumerEnum.contain(object.getConsumer_code())) &&
                object.getConsumer_code() != null)
            throw new Exception("消费者code错误!");

        if (!ProducerEnum.contain(object.getProducer_code()))
            throw new Exception("生产者code错误!");

        validate(object.getMessage_title(),"消息标题");

        validate(object.getMessage_content(),"消息内容");

        if (!LinkEnum.contain(object.getNotice_link_type()))
            throw new Exception("通知链接类型code错误!");

        if (!PushTypeEnum.contain(object.getTarget_code()))
            throw new Exception("推送类型code错误!");

        validate(object.getAuther(),"消息发布者");
    }

    private static boolean validate(String s,String index) throws Exception {
        if (StringUtils.isBlank(s))
            throw new Exception(index + "不能为空!");
        // xs
        if (s.replace(" ","").contains("<script"))
            throw new Exception("内容不能有非法字符!!!");
        //crsf
        //ip
        return true;
    }
}
